﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyBet.Controls
{
    public partial class CustomContestCreation : UserControl
    {
        public CustomContestCreation()
        {
            InitializeComponent();
            GenerateCodeButton.IsEnabled = false;
            SetCustomContestNameBackground();
            SetCustomContestPasswordBackground();
            SetCustomContestCodeBackground();
        }

        /// <summary>
        /// Demande de génération du code pour créer le tournoi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateCodeButton_Click(object sender, RoutedEventArgs e)
        {
            // Pré-requis : le tournoi doit faire plus de 6 caractères
            if (CustomContestName.Text.Trim().Length < 6)
            {
                MessageBox.Show("Le nom du tournoi doit faire plus de 6 caractères");
                return;
            }

            // Génération d'un password pour le tournoi
            // Ce mot de passe peut échouer si :
            // - Serveur injoignable
            // - Nom du tournoi est déjà utilisé

            SecurityServiceReference.SecurityServiceClient securitySvc = new SecurityServiceReference.SecurityServiceClient();
            securitySvc.GetContestPasswordCompleted += new EventHandler<SecurityServiceReference.GetContestPasswordCompletedEventArgs>(securitySvc_GetContestPasswordCompleted);
            securitySvc.GetContestPasswordAsync(App.CurrentUser.UserName, CustomContestName.Text.Trim());
        }

        void securitySvc_GetContestPasswordCompleted(object sender, SecurityServiceReference.GetContestPasswordCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("Un e-mail a été envoyé à votre adresse mail avec le code du tournoi.");
            }
            else
            {
                MessageBox.Show(e.myFault.Message);
            }
        }

        /// <summary>
        /// Renvoie True si le bouton "creer" est valide. Le bouton est valide si le nom du tournoi fait plus de 6 caractères 
        /// et que le mot de passe fait plus de 6 caractères
        /// </summary>
        /// <returns></returns>
        private bool IsCreationOK()
        { 
            return ((!String.IsNullOrWhiteSpace(CustomContestName.Text)) && 
                ((!String.IsNullOrWhiteSpace(CustomContestCode.Text)) && (CustomContestName.Text.Trim().Length > 6)) &&
                ((!String.IsNullOrWhiteSpace(CustomContestPassword.Password)) && (CustomContestPassword.Password.Trim().Length > 6)));
        }

        private bool IsGenerateOK()
        {
            return (String.IsNullOrWhiteSpace(CustomContestCode.Text))
                && (!String.IsNullOrWhiteSpace(CustomContestName.Text)
                && (CustomContestName.Text.Trim().Length > 6));
        }

        private bool IsCustomContestNameOK()
        { 
            return (!String.IsNullOrWhiteSpace(CustomContestName.Text) && (CustomContestName.Text.Trim().Length > 6));
        }

        private bool IsCustomContestPasswordOK()
        {
            return (!String.IsNullOrWhiteSpace(CustomContestPassword.Password) && CustomContestPassword.Password.Trim().Length > 6);
        }

        private bool IsCustomContestCodeOK()
        {
            return (!String.IsNullOrWhiteSpace(CustomContestCode.Text));
        }

        private void SetCustomContestNameBackground()
        {
            if (IsCustomContestNameOK())
            {
                CustomContestName.Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                CustomContestName.Background = new SolidColorBrush(MyBet.Class.ImageUtil.HexColor("FAB6B6"));
            }  
        }

        private void SetCustomContestPasswordBackground()
        {
            if (IsCustomContestPasswordOK())
            {
                CustomContestPassword.Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                CustomContestPassword.Background = new SolidColorBrush(MyBet.Class.ImageUtil.HexColor("FAB6B6"));
            }
        }

        private void SetCustomContestCodeBackground()
        {
            if (IsCustomContestCodeOK())
            {
                CustomContestCode.Background = new SolidColorBrush(Colors.White);
            }
            else
            {
                CustomContestCode.Background = new SolidColorBrush(MyBet.Class.ImageUtil.HexColor("FAB6B6"));
            }
        }

        private void CustomContestName_TextChanged(object sender, TextChangedEventArgs e)
        {
            GenerateCodeButton.IsEnabled = IsGenerateOK();
            CreateButton.IsEnabled = IsCreationOK();
            SetCustomContestNameBackground();
        }

        private void CustomContestCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            CreateButton.IsEnabled = IsCreationOK();
            GenerateCodeButton.IsEnabled = IsGenerateOK();
            SetCustomContestCodeBackground();
        }

        private void CustomContestPassword_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            CreateButton.IsEnabled = IsCreationOK();
            GenerateCodeButton.IsEnabled = IsGenerateOK();
            SetCustomContestPasswordBackground();
        }

        /// <summary>
        /// Création d'un nouveau tournoi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            BusyControl.IsBusy = true;
            SecurityServiceReference.SecurityServiceClient securitySvc = new SecurityServiceReference.SecurityServiceClient();
            securitySvc.CreateCustomContestCompleted += new EventHandler<SecurityServiceReference.CreateCustomContestCompletedEventArgs>(securitySvc_CreateCustomContestCompleted);
            securitySvc.CreateCustomContestAsync(App.CurrentUser.UserId, App.CurrentUser.UserName, App.DefaultContestId, CustomContestName.Text.Trim(), CustomContestCode.Text.Trim(), CustomContestPassword.Password.Trim());
        }

        void securitySvc_CreateCustomContestCompleted(object sender, SecurityServiceReference.CreateCustomContestCompletedEventArgs e)
        {
            BusyControl.IsBusy = false;
            if (e.Result)
            {
                MessageBox.Show("Le tournoi a été créé avec succès. Veuillez-vous déconnecter pour prendre en compte les modifications.");
            }
            else
            {
                MessageBox.Show(String.Format("{0}", e.myFault.Message));
            }
        }


    }
}
